//
//  KSAdBasePlayerView.m
//  Aspects
//
//  Created by xuzhijun on 2020/2/19.
//

#import "KSAdBasePlayerView.h"
#import "KSAdBasePlayerView+Internal.h"
#import "BlocksKit.h"

@implementation KSAdBasePlayerView {
    NSHashTable<id<KSAdBasePlayerViewDelegate>> *_delegates;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _delegates = [NSHashTable weakObjectsHashTable];
    }
    return self;
}

- (void)dealloc {

}

#pragma mark - Public
// 设置播放URL
- (void)setURL:(NSURL *)URL {
}

- (void)prepareWithURL:(NSURL *)URL {
}

// 指定到某个时间播放
- (void)resumeWithTime:(CGFloat)time {
}

- (void)seekWithTime:(CGFloat)time {
    
}

// 播放
- (void)play {
}

// 暂停
- (void)pause {
}

// 停止播放
- (void)stop {
}

- (void)changeCurrentURLAsset:(AVURLAsset *)urlAsset {
}

- (void)autoVideoGravity {
}

- (void)updateVideoGravity:(AVLayerVideoGravity)videoGravity {
}

- (void)replay {
}

- (void)addDelegate:(id <KSAdBasePlayerViewDelegate>)delegate {
    [_delegates addObject:delegate];
}

- (void)removeDelegate:(id <KSAdBasePlayerViewDelegate>)delegate {
    [_delegates removeObject:delegate];
}

- (NSArray<id <KSAdBasePlayerViewDelegate>> *)allDelegates {
    return _delegates.allObjects;
}

- (void)removeAllDelegates {
    [_delegates removeAllObjects];
}

#pragma mark - Readonly
- (CGFloat)duration {
    return 0;
}

- (CGFloat)currentTime {
    return 0;
}

- (BOOL)isPlaying {
    return NO;
}

- (BOOL)isFinish {
    return NO;
}
- (BOOL)canPlayOrPause {
    return YES;
}
- (AVURLAsset *)urlAsset {
    return nil;
}

- (void)resetPlayerParameter {
}

- (void)captureCurrentFrameWithCallback:(void (^)(UIImage *currentFrame))callback {
}

- (void)callDelegates:(void (^)(id<KSAdBasePlayerViewDelegate>))callBlock {
    if (!callBlock) {
        return;
    }

    [[self allDelegates] bk_each:^(id<KSAdBasePlayerViewDelegate> obj) {
        callBlock(obj);
    }];
}

- (void)changeVolume:(CGFloat)volume {
    NSAssert(NO, @"child class should implement changeVolume");
}

- (void)setVideoSoundEnable:(BOOL)videoSoundEnable isUserForceChange:(BOOL)userForceChange {
    if (![self shouldChangeVideoSoundStatusTo:videoSoundEnable] && !userForceChange) {
        return;
    }
    [self willChangeValueForKey:@"videoSoundEnable"];
    _videoSoundEnable = videoSoundEnable;
    [self didChangeValueForKey:@"videoSoundEnable"];
    if (videoSoundEnable) {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        CGFloat currentVol = audioSession.outputVolume;
        if (currentVol < 0.5f) {
            currentVol = 0.5f;
        }
        [self changeVolume:currentVol];
    } else {
        [self changeVolume:0];
    }
}

- (void)setVideoSoundEnable:(BOOL)enable {
    [self setVideoSoundEnable:enable isUserForceChange:NO];
}

- (void)updateVolume:(CGFloat)volume {
    if (![self shouldChangeVideoSoundStatusTo:volume != 0]) {
        return;
    }
    [self setVideoSoundEnable:volume != 0];
    [self changeVolume:volume];
}

- (BOOL)shouldChangeVideoSoundStatusTo:(BOOL)videoSoundEnable {
    return YES;
}

@end
